package ace.module.weixin.core.impl.service.impl;

import ace.cmp.core.enums.SystemCodeEnum;
import ace.cmp.core.exception.BusinessException;
import ace.module.weixin.api.model.dto.WxsGetPhoneNumberResultDto;
import ace.module.weixin.api.model.dto.WxsJsCode2SessionResultDto;
import ace.module.weixin.api.model.dto.WxsPhoneInfoDto;
import ace.module.weixin.core.impl.service.WxsMiniAppService;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.stereotype.Component;

/**
 * @author caspar
 * @date 2023/4/11 10:50
 */
@RequiredArgsConstructor
@Component
public class WxsMiniAppServiceImpl implements WxsMiniAppService {

  private final WxMaService wxMaService;

  @Override
  public WxsJsCode2SessionResultDto loginByCode(String appId, String code) {
    try {
      WxMaJscode2SessionResult wxMaJscode2SessionResult =
          wxMaService.switchoverTo(appId)
              .getUserService()
              .getSessionInfo(code);

      WxsJsCode2SessionResultDto resultDto = new WxsJsCode2SessionResultDto();
      resultDto.setOpenId(wxMaJscode2SessionResult.getOpenid());
      resultDto.setSessionKey(wxMaJscode2SessionResult.getSessionKey());
      resultDto.setUnionId(wxMaJscode2SessionResult.getOpenid());
      resultDto.setErrCode(SystemCodeEnum.SUCCESS.getCode());
      return resultDto;

    } catch (WxErrorException exception) {
      throw new BusinessException(
          exception.getError().getErrorCode(), exception.getError().getErrorMsgEn());
    }
  }

  @Override
  public WxsGetPhoneNumberResultDto getPhoneNumberByPhoneNumberCode(
      String appId, String phoneNumberCode) {
    this.wxMaService.switchoverTo(appId);
    WxMaPhoneNumberInfo wxMaPhoneNumberInfo = null;
    try {
      wxMaPhoneNumberInfo = wxMaService.getUserService().getNewPhoneNoInfo(phoneNumberCode);
    } catch (WxErrorException exception) {
      throw new BusinessException(
          exception.getError().getErrorCode(), exception.getError().getErrorMsgEn());
    }
    WxsGetPhoneNumberResultDto resultDTO = new WxsGetPhoneNumberResultDto();
    resultDTO.setPhoneInfo(
        WxsPhoneInfoDto.builder()
            .phoneNumber(wxMaPhoneNumberInfo.getPhoneNumber())
            .purePhoneNumber(wxMaPhoneNumberInfo.getPurePhoneNumber())
            .countryCode(wxMaPhoneNumberInfo.getCountryCode())
            .watermark(
                WxsPhoneInfoDto.Watermark.builder()
                    .appId(wxMaPhoneNumberInfo.getWatermark().getAppid())
                    .timestamp(
                        Long.valueOf(wxMaPhoneNumberInfo.getWatermark().getTimestamp()))
                    .build())
            .build());
    resultDTO.setErrCode(SystemCodeEnum.SUCCESS.getCode());
    resultDTO.setErrMsg(SystemCodeEnum.SUCCESS.getDesc());
    return resultDTO;
  }
}
